<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserPostRequest;

class UserController extends Controller
{
    /**
     * 展示后台用户列表
     *
     * @return object
     */
    public function getIndex(request $request)
    {
        $strip = $request->input('strip','10');

        $keywords = $request->input('keywords');

        if(isset($keywords)){
            $datas = DB::table('user')->where('username','like','%'.$keywords.'%')->paginate($strip);
        }else{
            $datas = DB::table('user')->paginate($strip);

        }
        $auth = ['普通管理员','超级管理员'];
        $status = ['禁止','开启'];
       return view('Admin.user.index',['datas'=>$datas,'pages'=>$request->all(),'auth'=>$auth,'status'=>$status]);
    }

    /**
     * 展示后台用户添加
     *
     * @return 添加用户视图
     */
    public function getAdd()
    {
       return view('Admin.user.add');
    }

    /**
     * 执行数据入库
     *
     * @return 数据ID
     */
    public function postInsert(UserPostRequest $request)
    {
       $data = $request->except(['_token','repassword']);

       //哈希加密
       $data['password'] = Hash::make($request->password);

        //调用方法执行文件上传
       $data['pic'] = $this->upload($request);

        //执行数据入库
       $num = DB::table('user')->insertGetId($data);

       if($num){
            return redirect('/admin/user/index')->with('success','用户添加成功');
       }else{
            return back()->with('error','用户添加失败');
       }
    }


    //执行文件上传
    public function upload($request)
    {
        //新文件名
        $name = md5(time()+rand(1,9999));

       

        //判断文件是否有上传
        if ($request->hasFile('pic')) {
             //获取文件后缀名
             $su = $request->file('pic')->getClientOriginalExtension();
            //移动文件
            $request->file('pic')->move('./public/uploads',$name.'.'.$su);

            //返回保存文件路径
            return '/public/uploads/'.$name.'.'.$su;
        }
    }

    /**
     *  执行删除操作
     *
     * @return 受影响行数
     */

    public function getDel($id)
    {
            $uid = session('uid');
          // 查询操作者权限
           $user = DB::table('user')->where('id',$uid)->first();

           if($user->auth == 1){

              $num =DB::table('user')->where('id',$id)->delete();
              if($num){ 
                return redirect('/admin/user/index')->with('success','用户删除成功');
              }else{
                return redirect('/admin/user/index')->with('error','用户删除失败');

              }
           }else{
                return redirect('/admin/user/index')->with('error','您的无权限进行删除操作,请联系超级管理员');
           }
    public function getDel(request $request,$id)
    {
        


            // $uid = $request->session()->('id');
           //查询操作者权限
           // $user = DB::table('user')->where('id',$uid)->first();

           // if($user->auth == 1){
                // DB::table('user')->where('id',$id)->delete();
                // return redirect('/admin/user/index')->with('success','用户删除成功');
           // }else{
                // return redirect('/admin/user/index')->with('error','用户删除失败');
           // }
        
    }

     /**
     * 展示后台用户修改
     *
     * @return object
     */
    public function getEdit($id)
    {
       //查询数据
       $user = DB::table('user')->where('id',$id)->first();

       return view('Admin.user.edit',['user'=>$user]);
    }

    /**
     * 执行修改
     *
     * @return 受影响行数
     */
    public function postUpdate(request $request)
    {
        //查询操作者权限
          $uid = session('uid');
          // 查询操作者权限
           $user = DB::table('user')->where('id',$uid)->first();

            if($user->auth == 1){
               //获取请求数据
               $data = $request->except(['_token','id']);

               $data['pic'] = $this->upload($request);

               $num = DB::table('user')->where('id',$request->input('id'))->update($data);

               if($num){
                    
                    return redirect('/admin/user/index')->with('success','用户修改成功');
               }else{
                    
                    return redirect('/admin/user/index')->with('error','用户修改失败');
               }
            }else{
                return redirect('/admin/user/index')->with('error','您的无权限进行删除操作,请联系超级管理员');
            }


       //获取请求数据
       $data = $request->except(['_token','id']);

       $data['pic'] = $this->upload($request);

       $num = DB::table('user')->where('id',$request->input('id'))->update($data);

       if($num){
            
            return redirect('/admin/user/index')->with('success','用户修改成功');
       }else{
            
            return redirect('/admin/user/index')->with('error','用户修改失败');
       }

    }

}
